package com.bosssoft.trainee.nontax.controller;

import com.bosssoft.trainee.nontax.common.Result;
import com.bosssoft.trainee.nontax.entity.po.News;
import com.bosssoft.trainee.nontax.mapper.NewsMapper;
import com.bosssoft.trainee.nontax.service.INewsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/news")
public class NewsController extends BaseController {

    @Resource
    NewsMapper newsMapper;
    @Resource
    private INewsService newsService;

    @PostMapping
    public Result<?> save(@RequestBody News news) {
        news.setTime(new Date());
        newsService.save(news);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody News news) {
        newsService.updateById(news);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> update(@PathVariable Long id) {
        newsService.removeById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Long id) {
        return Result.success(newsService.getById(id));
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        return newsService.findPage(pageNum, pageSize, search);
    }
}
